package project.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import project.model.RoomCategory;
import project.services.InventoryService;

@Controller
@RequestMapping("upload")
public class UploadController {

	@Autowired
	InventoryService inventoryService;
	
	public UploadController(){
	}
	
	@RequestMapping(value = {"/","filepage"},method = RequestMethod.GET)
	public String provideUploadInfo(){
		return "index";
	}
	
	@RequestMapping(value = "filepage",method = RequestMethod.POST)
	// with '@ResponseBody' annotation the String will not be resolved as an mapping name.
	public @ResponseBody String fileUpload(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request,
			HttpServletResponse response){
		long startTime = System.currentTimeMillis();
		String path = "./src/main/webapp/WEB-INF/uploads/" + file.getOriginalFilename();
		
		if(!file.isEmpty()){
			try{
				FileOutputStream outputStream = new FileOutputStream(path);
				InputStream inputStream = file.getInputStream();
				int b = 0;
				while((b=inputStream.read()) != -1)
					outputStream.write(b);
				
				outputStream.flush();
				inputStream.close();
				outputStream.close();
			}catch(FileNotFoundException e){
				e.printStackTrace();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		
		long endTime = System.currentTimeMillis();
		System.out.println("The total time of uploading a file is :" + (endTime - startTime));
		return "The file has been successfully uploaded!";
	}
	@RequestMapping(value = "excel",method = RequestMethod.GET)
	public @ResponseBody String excelResolver() throws IOException{
		File file = null;
		HSSFWorkbook workbook = null;
		FileInputStream fileInputStream = null;
		POIFSFileSystem fileSystem = null;
		HSSFSheet sheet = null;
		HSSFRow row = null;
		HSSFCell cell = null;
		HSSFCellStyle cellStyle = null;
		boolean quit = false;
		
		//result file
		file = new File("./src/main/webapp/WEB-INF/uploads/test.xls");
		fileInputStream = new FileInputStream(file);
		fileSystem = new POIFSFileSystem(fileInputStream);
		workbook = new HSSFWorkbook(fileSystem);
		
		//create a cell style
		cellStyle = workbook.createCellStyle();
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);  
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);  
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
		cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);  
		cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  

		RoomCategory category = new RoomCategory();
		sheet = workbook.getSheetAt(0);
		int i = 1;
		do {
			row = sheet.getRow(i);
			try {
				cell = row.getCell(0);
				category.setName(cell.getStringCellValue());
				cell = row.getCell(1);
				category.setDescription(cell.getStringCellValue());
			} catch (NullPointerException e) {
				quit = true;
			}
			inventoryService.addRoomCategory(category);
			i++;
		} while (!quit);
		row = sheet.getRow(0);
		cell = row.getCell(0);
		
		fileInputStream.close();
		return "Successfully done!";
	}
}
